﻿using System;
using System.ComponentModel;
using System.Globalization;
using Creventive.Wpf.ShellFactory.Workspace.Primitives;

namespace Creventive.Wpf.ShellFactory.Workspace
{
    /// <summary>
    ///   Represents a converter which can convert various source objects into an application view source.
    /// </summary>
    public class ApplicationViewSourceConverter : TypeConverter
    {
        /// <summary>
        ///   Returns whether this converter can convert an object of the given type to the type of this converter, using the specified context.
        /// </summary>
        /// <param name = "context">An <see cref = "T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context.</param>
        /// <param name = "sourceType">A <see cref = "T:System.Type" /> that represents the type you want to convert from.</param>
        /// <returns>
        ///   true if this converter can perform the conversion; otherwise, false.
        /// </returns>
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(IApplicationViewSource) || Array.IndexOf(sourceType.GetInterfaces(), typeof(IApplicationViewSource)) >= 0)
                return true;
            if (sourceType == typeof(Uri))
                return true;
            if (sourceType == typeof(string))
                return true;
            if (sourceType == typeof(ApplicationView))
                return true;
            return base.CanConvertFrom(context, sourceType);
        }

        /// <summary>
        ///   Converts the given object to the type of this converter, using the specified context and culture information.
        /// </summary>
        /// <param name = "context">An <see cref = "T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context.</param>
        /// <param name = "culture">The <see cref = "T:System.Globalization.CultureInfo" /> to use as the current culture.</param>
        /// <param name = "value">The <see cref = "T:System.Object" /> to convert.</param>
        /// <returns>
        ///   An <see cref = "T:System.Object" /> that represents the converted value.
        /// </returns>
        /// <exception cref = "T:System.NotSupportedException">The conversion cannot be performed. </exception>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is IApplicationViewSource)
                return value;
            if (value is Uri)
                return new ApplicationViewSource((Uri)value);
            if (value is string)
                return new ApplicationViewSource(new Uri((string)value, UriKind.RelativeOrAbsolute));
            if (value is ApplicationView)
                return new ApplicationViewSource((ApplicationView)value);
            return base.ConvertFrom(context, culture, value);
        }
    }
}